//
//  MLSVistorview.swift
//  MLSMircoBlog
//
//  Created by 袁航 on 15/7/29.
//  Copyright © 2015年 yuanhang. All rights reserved.
//

import UIKit
// 代理方法
protocol MLSVistorviewDelegate: NSObjectProtocol {
    func vistorviewShouldLogin()
    func vistorviewShouldRegister()
}

class MLSVistorview: UIView {
    
    //MARK:- 属性
    // 代理属性: 要用weak
    weak var delegate: MLSVistorviewDelegate?
    
    //MARK:- 系统方法
    // 重写init方法,加载布局控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        layoutSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        layoutSubview()
    }
    
    // MARK:- 自定义方法
    //MARK:- 登录按钮点击事件
    func loginBtnClick() {
        delegate?.vistorviewShouldLogin()
    }
    //MARK:- 注册按钮点击事件
    func registerBtnClick() {
        delegate?.vistorviewShouldRegister()
    }
    
    
    
    //MARK:- 全局共享设置控件内容
    func setUIInfo(isHome: Bool, centerImageName: String, tipString: String) {
        
        // 设置控件内容
        smallIcon.image = UIImage(named: centerImageName)
        tipLabel.text = tipString
        
        // 判断是否为主页,如果不是主页就隐藏小房子
        houseIcon.hidden = !isHome
        
        // 如果是主页,开始动画,如果不是主页,把遮罩放到后面
        isHome ? startAni() : sendSubviewToBack(backgroundImage)
        
    }
    
    //MARK:- 动画
    private func startAni() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 12
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        smallIcon.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK:- 布局控件
    private func layoutSubview() {
        addSubview(smallIcon)
        addSubview(backgroundImage)
        addSubview(houseIcon)
        addSubview(loginBtn)
        addSubview(registerBtn)
        addSubview(tipLabel)
        
        // 中央图片约束
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        // 背景图片约束
        addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        // 首页房子约束
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 提醒文字约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35.0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100.0))
        
        // 注册按钮登录
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100.0))
        
    }
    
    // MARK:- 懒加载控件
    // 背景图片
    private lazy var backgroundImage: UIImageView = {
        let backImage = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        backImage.translatesAutoresizingMaskIntoConstraints = false
        return backImage
        }()
    // 动画控件
    private lazy var smallIcon: UIImageView = {
        // 创建的同时初始化图片
        let smallIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        smallIcon.translatesAutoresizingMaskIntoConstraints = false
        return smallIcon
        }()
    
    // house
    private lazy var houseIcon: UIImageView = {
        let houseIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        houseIcon.translatesAutoresizingMaskIntoConstraints = false
        return houseIcon
        }()
    
    // 提示文字
    private lazy var tipLabel: UILabel = {
        let tipLabel = UILabel()
        tipLabel.numberOfLines = 0
        tipLabel.textColor = UIColor.darkGrayColor()
        tipLabel.text = "关注一些人，回这里看看有什么惊喜"
        tipLabel.font = UIFont.systemFontOfSize(14)
        tipLabel.translatesAutoresizingMaskIntoConstraints = false
        return tipLabel
        }()
    
    // 登录按钮
    private lazy var loginBtn: UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        loginBtn.setTitle("注册", forState: UIControlState.Normal)
        var image = UIImage(named: "common_button_white_disable")
        let edge = UIEdgeInsetsMake(image!.size.height * 0.5, image!.size.width * 0.5, image!.size.height * 0.5, image!.size.width * 0.5)
        image = image?.resizableImageWithCapInsets(edge, resizingMode: UIImageResizingMode.Stretch)
        loginBtn.setBackgroundImage(image, forState: UIControlState.Normal)
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加点击事件
        loginBtn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtn
        }()
    
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        let registerBtn = UIButton()
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setTitle("登录", forState: UIControlState.Normal)
        var image = UIImage(named: "common_button_white_disable")
        let edge = UIEdgeInsets(top:(image?.size.height)! * 0.5, left: (image?.size.width)! * 0.5, bottom: (image?.size.height)! * 0.5, right: (image?.size.width)! * 0.5)
        image = image?.resizableImageWithCapInsets(edge)
        registerBtn.setBackgroundImage(image, forState: UIControlState.Normal)
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return registerBtn
        }()
    
}
